#include "Menu.h"
#include <iostream>
//additional includes
#include <fstream>
#define DEBUG 1
#ifdef DEBUG
#include <cstdlib>
#endif
//#undef DEBUG

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{
	//--------RY_A3--------
	cout << "------------------------------------------------" << endl;
	cout << "Here is the MAIN MENU \n";
	cout << "Input menu number and press enter" << endl; 	//RY_A3
	cout << "1. Show Info" << endl;							//RY_A3
	cout << "2. Read" << endl;								//RY_A3
	cout << "3. Show Queries" << endl;						//RY_A3
	cout << "4. Find Queries" << endl;						//RY_A3
	cout << "5. Update Queries" << endl;					//RY_A3
	#ifdef DEBUG
	cout << "6. Error Check" << endl;						//RY_A3
	#endif
	cout << "7. Exit" << endl;								//RY_A3
	cout << "------------------------------------------------\n" << endl;
}

void Menu::display_info() const
{
	cout << "------------------------------------------------" << endl;
	cout << "Here is the Info requested ";	//display Project Part I, team name and team members
	//cout << "Team Details :";					// AM_A2.
	cout <<"\nTeam Name: Blue_VWX_16";			// AM_A2.
	cout <<"\nTeam Members:";					// AM_A2.
	cout <<"\n Member 1: Raydhitya Yoseph";		// AM_A2.
	cout <<"\n Member 2: Samuel C Carey";		// AM_A2.
	cout <<"\n Member 3: Alex Mathew\n";		// AM_A2.
	cout << "------------------------------------------------\n" << endl;
}


void Menu::read()
{
	cout << "------------------------------------------------" << endl;
	cout << "What data do you want to read?\n";
	cout << " 1) Customer data\n 2) Carts and Contents\n 3) Categories and Products\n 4) All Default Files\n> ";
	char choice;
	cin >> choice;
	string tFileName;
	string def = "d";
	if (choice=='1' || choice=='2' || choice =='3')
	{
		cout << "Input file name (or type 'd' for default file): ";	
		cin >> tFileName;
	}
	switch (choice)
	{
		case '1':
		{
			read_custs(tFileName);
			break;
		}
		
		case '2':			// AM_A2. See case 1 comments for explanations
		{
			read_carts_and_contents(tFileName);
			break;
		}
		
		case '3': //RY_A3
		{
			read_categories_and_products(tFileName);
			break;
		}
		
		case '4':
		{
			read_custs(def);
			read_carts_and_contents(def);
			read_categories_and_products(def);
			break;
		}
		
		default:
		{
			cout << "Please enter 1, 2, 3, or 4\n";
			read();
		}
	}
	cout << "------------------------------------------------" << endl;
}

void Menu::read_custs(string tFileName)
{	
	if (tFileName == "d") tFileName= "../Sample_Data/cust.dat";
	ifstream tFileStream;
	tFileStream.open(tFileName.c_str()); 	//RY open a file stream using the file name
	if (tFileStream.is_open()) {
		int size;
		int cust_id;
		string cust_name;
		int house;
		string street;
		int zip_code;
						
		tFileStream >> size;		//the first number is the number of entries in the list					
		int i;
		for (i=0 ; i<size ; ++i)
		{
			tFileStream >> cust_id			//catalogs entries from if stream
				>> cust_name
				>> house
				>> street
				>> zip_code;

			Address addr(house,street,zip_code);	//address entries into address type
			Customer cust(cust_id,cust_name,addr);	//address type and others into customer type
			custs.push_back(cust);					//customer type into customer vector
		}
		
		cout << "Customer data has been read.";	//RY
		cout << endl;
	} else {
		cout << "File does not exist" << endl;
		cout << "------------------------------------------------" << endl;
		read();
	}
}

void Menu::read_carts_and_contents(string tFileName)
{		
	if (tFileName == "d") tFileName= "../Sample_Data/carts.dat";
	
	ifstream tFileStream;
	tFileStream.open(tFileName.c_str()); 	//RY open a file stream using the file name
	if (tFileStream.is_open()) {
	
		int s;
		int cart_id,cust_id,year,month,day;
		//string file1="../Sample_Data/carts.dat";
		//ifstream ifs1 (file1.c_str());
		tFileStream >> s;
		int i;
		for(i=0;i<s;i++)
		{
			tFileStream >> cart_id
			 >> cust_id
			 >> year
			 >> month
			 >> day;
			Date cart_purchase_date(year,month,day);
			Cart crt(cart_id,cust_id,cart_purchase_date); //puts cart_purchase_date variable of Date type into crt
			carts.push_back(crt);
		}
		
		tFileStream >> s;
		int tCartItemId,tCartId,tProdId,tProdQty;
		for (i=0;i<s;i++)
		{
			tFileStream >> tCartItemId >> tCartId
						>> tProdId >> tProdQty;
			cart_items.push_back(Cart_item(tCartItemId,tCartId,tProdId,tProdQty));
		}
		cout << "Cart and content data has been read.";	//RY
		cout<<endl;
	} else {
		cout << "File does not exist" << endl;
		cout << "------------------------------------------------" << endl;
		read();
	}
}

void Menu::read_categories_and_products(string tFileName)
{
	if (tFileName == "d") tFileName= "../Sample_Data/catprd.dat";
	ifstream tFileStream;
	tFileStream.open(tFileName.c_str()); 	//RY open a file stream using the file name
	if (tFileStream.is_open()) {
		int t_cat_id,t_prod_id;
		string t_cat_name,t_prod_name;
		double t_prod_price;
		int number;
		tFileStream >> number;
		int i;
		for (i=0;i<number;i++)
		{
			tFileStream >> t_cat_id >> t_cat_name;
			cats.push_back(Category(t_cat_id,t_cat_name));
		}
		tFileStream >> number;
		
		for(i=0;i<number;i++)
		{
			tFileStream >> t_prod_id >> t_cat_id >> t_prod_name >> t_prod_price;
			//Product t_prod(t_prod_id,t_cat_id,t_prod_name,t_prod_price);
			prods.push_back(Product(t_prod_id,t_cat_id,t_prod_name,t_prod_price));
			//prods.push_back(t_prod);
		}
		cout << "Category and product data has been read." << endl;	//RY
	} else {
		cout << "File does not exist" << endl;
		cout << "------------------------------------------------" << endl;
		read();
	}
}

void Menu::show() const 
{
	cout << "------------------------------------------------" << endl;
	cout << "Show queries \n";
	char choice;
	cout 	<< "What do you want to see?\n"
			<< " 1) Customer Data\n"
			<< " 2) Products Data\n"
			<< " 3) Categories and Products\n"
			<< " 4) Cart products and costs\n"
			<< "> ";
	cin >> choice;
	switch (choice)
	{
		case '1':
		{
			ShowFindUpdateSCC::show(custs);
			//RY above method still hasn't produced what the assignment told to do 
			//OtherSCC::show(custs);
			break;
		}
		case '2': 
		{
			ShowFindUpdateAM::show(prods);
			break;
		}
		case '3':
		{
			ShowFindUpdateRY::show_cats(cats,prods);
			break;
		}
		case '4':
		{
			//RY
			//OtherSCC::show() is the same with ShowFindUpdateSCC::show()
			//OtherSCC::show(custs, carts, cart_items, prods);
			break;
		}
		default: 
		{
			cout << "Please enter a 1, 2, or 3" << endl;
			show();
		}
	}
	cout << "------------------------------------------------" << endl;
}
void Menu::find() const 
{
	cout << "------------------------------------------------" << endl;
	cout << "Find queries \n";
	char choice;
	cout << "What do you want to see?\n 1) Total Sales for Customer\n 2) Total Sales for Product\n 3) Total Sales for Category\n> ";
	cin >> choice;
	switch (choice)
	{
		case '1':
		{
			ShowFindUpdateSCC::find(custs, cart_items, carts, prods);
			break;
		}
		case '2':
		{
			ShowFindUpdateRY::find(cart_items,prods);			
			break;
		}
		case '3':
		{
			//ShowFindUpdateAM::find();
			cout << "not yet" << endl;
			break;
		}
		default: cout << "Please enter a 1, 2, or 3" << endl;
	}
	cout << "------------------------------------------------" << endl;
}

void Menu::update()  
{
	cout << "------------------------------------------------" << endl;
	cout << "Update queries \n";
	char choice;
	cout 	<< "What would you like to update?\n"
			<< " 1) Customers\n"
			<< " 2) Carts\n"
			<< " 3) Product and category\n"
			<< "> ";
	cin >> choice;
	switch (choice)
	{
		case '1':
		{
			ShowFindUpdateSCC::update(&custs);
			break;
		}
		case '2':
		{
			printCarts();
			cout 	<< "  1) Add item to cart\n"
					<< "  2) Delete item from cart\n"
					<< "  3) Add a new cart\n"
					<< "> ";
			cin >> choice;
			switch (choice)
			{
				case '1':
				{
					//RY i completed the method
					ShowFindUpdateAM::update_add_cart_item(cart_items);
					break;
				}
				case '2':
				{
					OtherSCC::delete_cart_item(carts, custs, prods, &cart_items);
				}
				case '3':
				{
					ShowFindUpdateSCC::show(custs);
					OtherSCC::update(&carts, custs);
					printCarts();
					break;
				}
			}
			break;
		}
		case '3':
		{
			cout 	<< "  1) Add a product for a category\n"
					<< "  2) Delete a product for a category\n"
					<< "  3) Change price of a product\n"
					<< "  4) Add a category\n"
					<< "> ";
			cin >> choice;
			switch (choice)
			{
				case '1': 
				{
					ShowFindUpdateRY::update(cats,&prods);
					break;
				}
				case '2':
				{
					OtherAM::delete_prod(&prods,cats);
					break;
				}
				case '3':
				{
					OtherAM::change(&prods);
					break;
				}
				case '4':
				{
					OtherAM::update(&cats);
					break;
				}
			}
			break;
		}
		default: cout << "Please enter a 1, 2, or 3" << endl;	
	}
	cout << "------------------------------------------------" << endl;	
}

void Menu::write() const
{	
	cout << "writing file...." << endl;
	ofstream tFileStream;
	
	//customer data
	tFileStream.open("cust1.dat");
	int i;
	tFileStream << custs.size() << endl;
	for (i=0;i<custs.size();i++)
	{
		tFileStream << custs.at(i).display() << endl;
	}
	tFileStream.close();
	
	//carts data
	tFileStream.open("carts1.dat");
	tFileStream << carts.size() << endl;
	for (i=0;i<carts.size();i++)
	{
		tFileStream << carts.at(i).display() << endl;
	}
	tFileStream << endl;
	
	//cart items data
	tFileStream << cart_items.size() << endl;
	for (i=0;i<cart_items.size();i++)
	{
		tFileStream << cart_items.at(i).display() << endl;
	}
	tFileStream.close();
	
	//categories data
	tFileStream.open("cartprd1.dat");
	tFileStream << cats.size() << endl;
	for (i=0;i<cats.size();i++)
	{
		tFileStream << cats.at(i).display() << endl;
	}
	tFileStream << endl;
	tFileStream << endl;
	
	//products data
	tFileStream << prods.size() << endl;
	for (i=0;i<prods.size();i++)
	{
		tFileStream << prods.at(i).display() << endl;
	}
	tFileStream.close();
	
	cout << "finish writing file." << endl;
}


void Menu::errorcheck() const
{
	cout << "------------------------------------------------" << endl;	
	cout << "error checking..." << endl;
	cout << (OtherRY::isCartItemValid(cart_items, prods) ? 
		"every cart items refers to a existent product" : "at least one cart items refers to a non-existent product") << endl;
	cout << (OtherRY::isCartValid(carts, cart_items) ?
		"every cart has at least one cart item" : "at least one cart items does not have at least one cart item") << endl;
	cout << (OtherRY::isProductValid(prods) ?
		"every products have price greater than zero" : "at least one product has price less than zero") << endl;
	cout << "error checking done" << endl;
	cout << "------------------------------------------------" << endl;	
}

//RY to check the change in carts
void Menu::printCarts() const
{
	cout << "------------------------------------------------" << endl;
	cout << "cart_id\tcust_id\tcart_purchase_date"<<endl;
	int iCarts;
	for (iCarts=0;iCarts<carts.size();iCarts++)
	{
		cout << carts.at(iCarts).display() << endl;
	}
	cout << "------------------------------------------------" << endl;
}
